import request from "../request.js"
import cfg from "../config.js"

let page_type = {
    open: function (page_info) {
        let page_html_id=page_info.page_html_id;
        let property=page_info.property;
        let ext_js=page_info.ext_js;
        let page_id=page_info.page_id;
        let rights=page_info.rights;
        let params=page_info.params;
        $("#" + page_html_id).addClass("curd-quick-builder-page row");
        $("#" + page_html_id).html("\
        <div class='col-md-6 col-sm-12 col-xs-12' style='padding:0.8rem;'>\
            <div class='view-part view-add-part'></div>\
            <div class='view-part view-table-part'></div>\
        </div>\
            <div class='col-md-6 col-sm-12 col-xs-12' style='padding:0.8rem;'>\
                <div class='config-part'></div>\
            </div>");

        let amisJSON = {
            "type": "page",
            "name": page_html_id + "_config",
            "body": [
                {
                    "type": "form",
                    "name": "config_form",
                    "api": cfg.api_server + "crudbuilder/quickbuild",
                    "title": "CRUD配置",
                    "resetAfterSubmit": true,
                    "redirect": "index.html", //?pid=${page_id}
                    "persistData": "curd-quick-builder-page",
                    "clearPersistDataAfterSubmit": true,
                    "multiple": true,
                    "className": "m-t",
                    "actions": [
                      {
                        "type": "reset",
                        "label": "重置"
                      },
                      {
                        "type": "submit",
                        "level": "primary",
                        "label": "一键生成整套CRUD功能"
                      }
                    ],
                    "controls": [
                        {
                            "type": "service",
                            "name": "configer",
                            "data": {
                                "api_server": cfg.api_server
                            },
                            "schemaApi": {
                                "method": "post",
                                "url": cfg.api_server + "configer/getbyname",
                                "requestAdaptor": function (api) {
                                    api.data.name = "crud_quick_builder";
                                },
                                "adaptor": function (payload, response) {
                                    return payload;
                                }
                            }
                        }
                    ]
                }
            ]
        }
        let amis = amisRequire('amis/embed');
        let config_amisScoped = amis.embed(
            '#' + page_html_id + " .config-part", amisJSON, {},
            {
                theme: 'antd'
            }
        );

        let refresh = function () {
            if ($('#' + page_html_id + " .view-part").length == 0) return;
            let data = config_amisScoped.getComponentByName(page_html_id + "_config.config_form").props.data;
            let fields = data.fields?data.fields:[];
            let add_form_controls = [];
            let add_form_controls_group = [];
            let pre_group_no;
            let pre_group;
            let columns = [{
                name: "id",
                label: "ID",
                searchable: true
            }];
            for (let field of fields) { //all fields
                let name = field.field_name;
                let label = field.name;
                let type = field.type;
                let col = {
                    name: name,
                    label: label,
                    searchable: true
                }
                columns.push(col);
                let add_form_control = {
                    type: "text",
                    name: name,
                    label: label,
                    required: field.required
                }
                if (type == "textarea") {
                    add_form_control.type = "textarea";
                }
                else if (type == "number") {
                    add_form_control.type = "input-number";
                }
                else if (type == "date") {
                    add_form_control.type = "input-date";
                }
                else if (type == "datetime") {
                    add_form_control.type = "input-datetime";
                }
                else if (type == "switch") {
                    add_form_control.type = "switch";
                }
                else if (type == "file") {
                    add_form_control.type = "input-file";
                }
                else if (type == "image") {
                    add_form_control.type = "input-image";
                }
                else if (type == "password") {
                    add_form_control.type = "input-password";
                }
                else if (type == "select") { //下拉选框
                    add_form_control.type = "select";
                }
                else if (type == "config") { //属性编辑框
                    add_form_control.type = "json-editor";
                }
                else if (type == "related") { //关联表选择
                    add_form_control.type = false ? "tree-select" : "picker";
                }
                //Form的分组处理
                let group_no = field.group_no;
                if (group_no) {
                    if(group_no!=pre_group_no){
                        let new_group_no = group_no + "_" + Math.random();
                        add_form_controls_group[new_group_no] = [];
                        pre_group=add_form_controls_group[new_group_no];
                    }
                    pre_group.push(add_form_control);
                }
                else {
                    let group_no = "not_in_group_" + Math.random();
                    add_form_controls_group[group_no] = [add_form_control];
                    pre_group=add_form_controls_group[group_no];
                }
                pre_group_no=group_no;
            }
            columns.push({
                name: "create_time",
                label: "创建时间",
                searchable: true
            })
            //对添加窗口的分组处理
            for (let group_controls of Object.values(add_form_controls_group)) {
                let group = {
                    "type": "group",
                    "controls": group_controls
                };
                add_form_controls.push(group);
                add_form_controls.push({ "type": "divider" }); //分割线
            }
            let addJSON = {
                "type": "form",
                "title": "",
                "submitText": "",
                "mode": "normal",
                "controls": add_form_controls
            }
            let add_amisScoped = amis.embed(
                '#' + page_html_id + " .view-add-part", addJSON, {},
                {
                    theme: 'antd'
                }
            );
            let tableJSON = {
                "type": "crud",
                "affixHeader": false,
                "syncLocation": false,
                "perPage": 10,
                "footerToolbar": [
                    "statistics",
                    "switch-per-page",
                    "pagination"
                ],
                "columns": columns
            }
            let table_amisScoped = amis.embed(
                '#' + page_html_id + " .view-table-part", tableJSON, {},
                {
                    theme: 'antd'
                }
            );
        }

        //定时同步刷新预览
        let interval = window.setInterval(refresh, 500);
    }
}

export default page_type;